﻿package com.peachcastle.core 
{
	import flash.events.EventDispatcher;
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.filters.BlurFilter;
	import flash.display.Sprite;
	import flash.display.MovieClip;
	
	import com.peachcastle.core.GameLoader;
	import com.peachcastle.tools.Utils;
	
	import com.peachcastle.interfaces.IDialog;
	/**
	 * ...
	 * @author kevin
	 */
	public class Dialog extends EventDispatcher implements IDialog
	{
		private var _loader:GameLoader = null;				//加载器
		private var _loaderContext:LoaderContext = null;	//存储对象
		
		private var _isPreload:Boolean = false;				//是否预加载
		private var _isLoading:Boolean = false;				//是否在加载
		private var _isReady:Boolean = false;				//是否准备好UI
		private var _isHide:Boolean = true;
		private var _isInited:Boolean = false;				//是否初始化
		
		private var _background:Sprite = null;				//遮罩;
		private var _assest:MovieClip = null;				//对话框UI
		
		private var _className:String = "";					//类名
		private var _recPathUrl:String = "";				//地址
		private var _classPackagePrefix:String = "dialogs.";//包名
		
		private var _indexw:Number = 0;
		private var _indexh:Number = 0;
		
		private var _fiter:BlurFilter;
		
		private var _dialogUrlFunc:Function = null;
		private var _stageWidth:int = 0;
		private var _stageHeight:int = 0;
		private var _isback:Boolean = true;
		
		/**
		 * 
		 * @param	assestClassName   		类名
		 * @param	isback				  	是否显示背景遮罩;
		 */
		public function Dialog(assestClassName:String = "", isback:Boolean = true, dialogUrlFunc:Function = null, stageWidth:Number = 0, stageHeight:Number = 0) 
		{
			_loader = new GameLoader();
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete, false, 0, true);
			_loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);
			_loaderContext = new LoaderContext(true, ApplicationDomain.currentDomain);
			
			if (dialogUrlFunc != null) _dialogUrlFunc = dialogUrlFunc;
			setStage(stageWidth, stageHeight);

			_isback = isback;
			
			setDialogData(assestClassName);

			_fiter = new BlurFilter(5, 5);
			
		}	
		
		public function setStage(width:Number, height:Number):void
		{
			_stageWidth = width;
			_stageHeight = height;
		}
		
		/**
		 * 居中
		 */
		public function setCenter():void
		{
			
			var dy:Number = 20 + ((_stageHeight -_indexh)>>1)
			move(_centerX, dy);
		}
		
		protected function setDialogData(assestClassName:String):void		
		{
			_className = assestClassName;
			if (_dialogUrlFunc != null) {
				_recPathUrl = _dialogUrlFunc(lowerCapFirstChar(assestClassName));
			}else {
				_recPathUrl = lowerCapFirstChar(assestClassName);
			}
		}
		
		private function getDialogRec():void
		{			
			var flag:Boolean = ApplicationDomain.currentDomain.hasDefinition(_classPackagePrefix + _className);			
			if (flag) return;
			
			loadRec(false);
		}		
		
		/**
		 * 加载中..
		 * @param	event
		 */
		protected function onProgress(event:ProgressEvent):void
		{
			
		}
		
		/**
		 * 加载完成
		 * @param	event
		 */
		private function onComplete(event:Event):void
		{
			_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete);
			_loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, onProgress);
			
			event.stopPropagation();
			
			_isLoading = false;
			_isReady = true;
			
			_assest = getRecClip(_classPackagePrefix + _className);
			_assest.cacheAsBitmap = true;
			Utils.stopClip(_assest);
			
			_indexw = _assest.width;
			_indexh = _assest.height;
			
			_centerX = (_stageWidth - _indexw) >> 1;
			_centerY = (_stageHeight - _indexh ) >> 1;
			if (_isPreload) 
			{
				return;		
			}
			//
			delayShowDialog();
		}
		
		/**
		 * 预加载;
		 */
		
		public function preLoadRec():void
		{
			loadRec(true);
		}
		
		public function loadRec(isPreload:Boolean = false):void
		{
			_isPreload = isPreload;
			
			if (_isLoading || _assest != null) return;
			
			if(!_isLoading)
			{
				if (_isReady && _isPreload == false) { delayShowDialog(); return }	
				_isLoading = true;
			}
			
			_loader.loadRec(_recPathUrl, _loaderContext);
		}
		
		/**
		 * 初始化
		 */
		protected function initialization():void
		{
			if (!_isback) return;
			_background = new Sprite();
			_background.graphics.beginFill(0x000000, .5);
			
			_background.graphics.drawRect(0, 0, layer.stage.stageWidth, layer.stage.stageHeight);
			_background.graphics.endFill();
			
			assest.addChildAt(_background, 0);
		}	
		
		/**
		 * 添加事件
		 */
		protected function configureEventListeners():void
		{
			
		}
		
		/**
		 * 延迟显示
		 */
		private function delayShowDialog():void
		{
			if (_isHide) return;
			
			if (!_isInited)
			{
				_isInited = true;
				initialization();
				configureEventListeners();
				
			}
			
			layer.addChild(assest);
			showAction();
		}	
		
		/**
		 * 定位;
		 * @param	dx
		 * @param	dy
		 */
		protected function move(dx:Number=0, dy:Number=0):void
		{
			_assest.x = dx;
			_assest.y = dy;
			if (_background)
			{
				_background.x = -dx;
				_background.y = -dy;
			}
		}
		
		/**
		 * 显示执行
		 */
		protected function showAction():void
		{

		}
		
		/**
		 * 隐藏执行
		 */
		protected function hideAction():void
		{

		}
		
		/**
		 * 显示；
		 */
		public function show(value:Object = null):void
		{
			_isHide = false;
			_data = value;

			if (_assest != null)
			{
				delayShowDialog();
				return;
			}

			getDialogRec();
		}
		
		/**
		 * 隐藏;
		 */
		public function hide():void
		{
			if (isHide) return;

			_isHide = true;
			hideAction();
			
			if (assest&&assest.parent) assest.parent.removeChild(assest);
		}
		
		/**
		 * 获取资源剪辑
		 * @param	className
		 * @return
		 */
		protected function getRecClip(className:String):MovieClip
		{				
			var ClipClass:Class = ApplicationDomain.currentDomain.getDefinition(className) as Class;
			return (new ClipClass()) as MovieClip;
		}
		
		/**
		 * 将首写字母转为小写
		 * @param	content
		 * @return
		 */
		private function lowerCapFirstChar(content:String):String
		{
			return content.charAt(0).toLowerCase() + content.substr(1);
		}	
		
		public function get assest():MovieClip { return _assest; }
		
		private var _layer:DisplayObjectContainer = null;
		public function get layer():DisplayObjectContainer { return _layer; }
		public function set layer(value:DisplayObjectContainer):void { _layer = value; }
		
		public function get isHide():Boolean { return _isHide; }
		
		private var _centerX:Number;
		public function get centerX():Number { return _centerX; }
		private var _centerY:Number;
		public function get centerY():Number { return _centerY; }
		
		private var _data:Object = null;
		public function get data():Object { return _data; }
		
		public function get stageWidth():Number { return _stageWidth; }
		public function get stageHeight():Number { return _stageHeight; }
	}

}